define(['jquery', 'bootstrap', 'mbti', 'form', 'template', 'flatpickr', 'flatpickr-lang'], function ($, undefined, Mbti, Form, Template, flatpickr, flatpickrLang) {
    var Controller = {
        evaluateId: 0,
        sceneId: 0,
        index: function () {
        },
        detail: function () {
            Controller.sceneId = Config.sceneId;
            Controller.evaluateId = Config.evaluateId;
            Controller.api.bindevent();
        },
        sms_tool: function() {
            $(document).ready(function(){
                Controller.api.smsSend(0, Config.phones, Config.sms_page, Config.sms_max_page);
            });
        },
        api: {
            smsSend: function(index=0, phones=[], sms_page=0, max_page=0) {
                if(phones.length > 0){
                    if(index >= phones.length){
                        if(sms_page == max_page){
                            layer.alert('发送完成');
                        }else{
                            window.location.href = Fast.api.fixurl('index/sms_tool?page='+(sms_page+1));
                        }
                    }else{
                        const currData = phones[index];
                        const serviceProvider = {"CT":"中国电信", "CM":"中国移动", "CU":"中国联通"};
                        const html = '<div class="row"><div class="col-xs-2 col-md-2 text-center">('+currData.id+')'+currData.name+'</div><div class="col-xs-2 col-md-2 text-center">'+serviceProvider[currData.service_provider]+'</div><div class="col-xs-2 col-md-2 text-center">'+currData.mobile+'</div><div class="col-xs-2 col-md-2 text-center" id="sms_status_'+currData.id+'"><span class="label label-info">发送中...</span></div><div class="col-xs-4 col-md-4 text-center" id="sms_result_'+currData.id+'"><span class="label label-info">发送中...</span></div></div>';
                        $("#sms_list").append(html);
                        const currDom = document.getElementById('sms_'+currData.id);
                        $.ajax({
                            url: Fast.api.fixurl('index/sms_send'), // 请求的URL
                            type: 'post', //请求类型,若为post,则表示发送post请求
                            data: {
                                id: parseInt(currData.id),
                                mobile: currData.mobile,
                                template: "register"
                            },     // 请求携带数据
                            dataType: 'json',  // 接收的数据类型
                            isAsync: false,     // 是否异步请求
                            success: function(data) {
                                if(data.code == 1){
                                    document.getElementById('sms_status_'+currData.id).innerHTML = ('<span class="label label-success">'+data.data+'</span>');
                                    document.getElementById('sms_result_'+currData.id).innerHTML = ('<span class="label label-success">'+data.data+'</span>');
                                }else{
                                    document.getElementById('sms_status_'+currData.id).innerHTML = ('<span class="label label-danger">'+(data.data ? data.data : "发送失败")+'</span>');
                                    document.getElementById('sms_result_'+currData.id).innerHTML = ('<span class="label label-danger">'+data.msg+'</span>');
                                }
                                setTimeout(function(){
                                    Controller.api.smsSend((index+1), phones, sms_page, max_page);
                                },2000);
                            },
                            error: function(err) {
                                document.getElementById('sms_status_'+currData.id).innerHTML = ('<span class="label label-danger">请求失败</span>');
                                document.getElementById('sms_result_'+currData.id).innerHTML = ('<span class="label label-danger">'+err+'</span>');
                                setTimeout(function(){
                                    Controller.api.smsSend((index+1), phones, sms_page, max_page);
                                },2000);
                            }
                        });
                    }
                }else{
                    if(sms_page == max_page){
                        layer.alert('发送完成');
                    }else{
                        window.location.href = Fast.api.fixurl('index/sms_tool?page='+(sms_page+1));
                    }
                }
            },
            bindevent: function () {
                //Form.events.datetimepicker($("form[role=form]"));
                flatpickr.localize(flatpickr.l10ns.zh);
                $("#flatpickr").flatpickr({
                    dateFormat: $("#flatpickr").data("format"),
                    minDate: $("#flatpickr").data('min-date'),
                    maxDate: $("#flatpickr").data('max-date'),
                    defaultDate: new Date($("#flatpickr").data('default-date'))
                });
                const appletBtn = document.getElementById('launch-btn');
                if(Controller.evaluateId > 0){
                    if(appletBtn) {
                        appletBtn.setAttribute("path", Config.applet_path + "?scene_id=" + parseInt(Controller.sceneId) + "&evaluate_id=" + parseInt(Controller.evaluateId));
                    }
                }
                if(Config?.wx_config){
                    wx.config(Config?.wx_config);
                    wx.ready(function(){
                        wx.updateAppMessageShareData({
                            ...Config?.wx_share,
                            success: function (res) {
                                console.log("------------设置成功------------", res);
                            }
                        });
                        wx.updateTimelineShareData({
                            ...Config?.wx_share,
                            success: function () {
                                console.log("------------设置分享朋友圈成功------------", res);
                            }
                        });
                        if(wx?.miniProgram?.getEnv){
                            wx?.miniProgram?.getEnv(function(res) {
                                if(res?.miniprogram){
                                    $("#show_result").click(function(){
                                        wx?.miniProgram?.redirectTo({
                                            url:'/pages/mbti/result?scene_id='+parseInt(Controller.sceneId)+'&evaluate_id='+parseInt(Controller.evaluateId)
                                        });
                                    });
                                }else{
                                    appletBtn.addEventListener('launch', function (e) {
                                        wx.closeWindow();
                                    });
                                    appletBtn.addEventListener('error', function (e) {
                                        console.log('fail', e.detail);
                                    });
                                    $("#show_result").replaceWith(appletBtn);
                                    $(appletBtn).show();
                                }
                            });
                        }else{
                            appletBtn.addEventListener('launch', function (e) {
                                wx.closeWindow();
                            });
                            appletBtn.addEventListener('error', function (e) {
                                console.log('fail', e.detail);
                            });
                            $("#show_result").replaceWith(appletBtn);
                            $(appletBtn).show();
                        }
                    });
                }else{
                    appletBtn.addEventListener('launch', function (e) {
                        wx.closeWindow();
                    });
                    appletBtn.addEventListener('error', function (e) {
                        console.log('fail', e.detail);
                    });
                    $("#show_result").replaceWith(appletBtn);
                    $(appletBtn).show();
                }
            }
        }
    };
    return Controller;
});